package com.aji.alluser.service.impl;
import com.aji.allcommon.utills.JwtUtil;
import com.aji.alluser.domain.User;

import com.aji.alluser.domain.LoginUser;
import com.aji.alluser.domain.dto.LoginDto;
import com.aji.alluser.domain.vo.UserInfo;
import com.aji.alluser.exception.AccountNotFoundException;
import com.aji.alluser.mapper.LoginMapper;
import com.aji.alluser.properties.JwtProperties;
import com.aji.alluser.service.LoginService;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
@Slf4j
public class LoginServiceImpl implements LoginService {
    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    LoginMapper loginMapper;
    @Autowired
    private JwtProperties jwtProperties;
    @Override
    public User login(LoginDto loginDto) {
        //用authenticationManager进行用户认证
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginDto.getUsername(),loginDto.getPassword());
        Authentication authenticate = authenticationManager.authenticate(authenticationToken);
        if(authenticate == null){
            throw new AccountNotFoundException("用户找不到！");
        }
        LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
        return loginUser.getUser();
//        String username = loginDto.getUsername();
//        String password = loginDto.getPassword();
//        User user = loginMapper.getByUserName(username);
//        if(user == null){
//            throw new AccountNotFoundException("用户找不到！");
//        }
//        password = DigestUtils.md5DigestAsHex(password.getBytes());
//        if(!password.equals(user.getPassword())){
//            throw new PasswordErrorException("密码错误");
//        }
//
//        if(user.getStatus() == 0){
//            throw new AccountLockedException("用户被锁定");
//        }
//        return user;
    }

    @Override
    public List<String> getRoleById(Long id) {
        return loginMapper.getRoleById(id);
    }

    @Override
    public User getByUserId(Long principal) {
        return loginMapper.getByUserId(principal);
    }

    @Override
    public List<String> selectPermsByUserId(Long id) {
        return loginMapper.selectPermsByUserId(id);
    }

    @Override
    public UserInfo userInfo(String token) {
                Claims claims = JwtUtil.parseJWT(jwtProperties.getAdminSecretKey(), token);
        Long userId = Long.valueOf(claims.get("userId").toString());

        User user = getByUserId(userId);
        UserInfo userInfo = UserInfo.builder()
                .permission(getRoleById(user.getId()))
                .name(user.getName())
                .phone(user.getPhone())
                .school(user.getSchool()).build();
        return userInfo;
    }


}
